package org.tln.db137.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tln.db137.common.R;
import org.tln.db137.entity.Role;
import org.tln.db137.service.RoleService;

import java.util.List;

@RestController
@CrossOrigin("*")
public class RoleController {
    @Autowired
    private RoleService roleService;

    //查询所有角色
    @GetMapping("selectAllRole")
    public R selectAllRole() {
        List<Role> roleList = roleService.list();

        return new R().add("roleList", roleList);
    }
    //新增角色
    @PostMapping("insertrole")
    public Object insertrole(@RequestBody Role role) {
        return this.roleService.save(role);
    }
    //修改角色
    @PostMapping("updaterole")
    public Object updaterole(@RequestBody Role role) {
        return this.roleService.updateById(role);
    }
    //删除角色
    @PostMapping("deleterole")
    public Object deleterole(@RequestBody Role role) {
        QueryWrapper<Role> wrapper=new QueryWrapper<>();
        wrapper.eq("id",role.getId());
        return this.roleService.remove(wrapper);
    }

    /**
     * 根据用户id获取拥有的角色集合
     * @return
     */
    @GetMapping("/selectRoleByuserId/{userId}")
    public R selectRoleByuserId(@PathVariable("userId") int userId) {
        List<Role> roleList=this.roleService.selectRoleByuserId(userId);
        return new R().add("roleList", roleList);
    }
}
